package com.javaspeak.designpatterns.go4.behavioural.command;

import java.util.HashMap;
import java.util.Map;

/**
 * The Command pattern can be seen in web frameworks like the Struts framework.
 * <p>
 * In the Struts framework different urls are mapped to different Commands.
 * A centralised Controller called the Dispatcher Controller delegates to a
 * particular Command object to do some processing when it receives a particular
 * url.  After the Command has finished processing the request and
 * attributes are forwarded to a JSP servlet which merges HTML and the
 * attributes into the response.
 * <p>
 * This example shows similarity to the Struts framework:
 * <p>
 * A call is made to the processRequest method of the RequestProcessor passing
 * it a map of attributes.
 * <p>
 * The RequestProcessor retrieves the RequestType from  the request and
 * executes a command which is mapped to that RequestType.
 * <p>
 * The Command class reads the map of attributes and creates a modified new
 * map of the attributes which it calls the handleResponse(..) method of
 * the Response with. The handleResponse method prints the modified attributes
 * to System.out.
 * <p>
 * This example calls the RequestProcessor twice, each time with a different
 * RequestType.
 * <p>
 * The first RequestType is RequestType.CAPITALISE and the RequestProcessor
 * calls the CapitaliseCommand.
 * <p>
 * The second RequestType is RequestType.SORT and the requestProcessor calls the
 * SortCommand
 * <p>
 * Both the calls to the RequestProcessor use the same ResponseHandler which
 * prints the following to System.out:
 *
 *   ===============================================
 *   message : HELLO WORLD!
 *   quote : COMMON SENSE IS NOT COMMON
 *   name : JOHN DICKERSON
 *   ===============================================
 *   quote : Common Sense is not Common
 *   message : Hello World!
 *   name : John Dickerson
 *   ===============================================
 *
 * Notice that the first command capitalised the text and the second command
 * sorted the attribute keys by the attribute values.
 *
 * @author John Dickerson
 */
public class ApplicationCommand {

    public  void runExample(){

        RequestProcessor requestProcessor = new RequestProcessorImpl();

        Long requestId = 1L;
        Map<String,String> requestAttributes = new HashMap<String,String>();
        requestAttributes.put( "name", "John Dickerson" );
        requestAttributes.put( "message", "Hello World!" );
        requestAttributes.put( "quote", "Common Sense is not Common" );

        Request request =
            new RequestImpl(
                   requestId, RequestType.CAPITALISE, requestAttributes);

        Response response =
            new Response(){

                @Override
                public void handleResponse(
                        Map<String, String> responseAttributes) {

                    for ( String key: responseAttributes.keySet() ){

                        System.out.println(
                             key + " : " + responseAttributes.get( key ) );
                    }
                }
            };

        System.out.println( "===============================================" );

        requestProcessor.processRequest( request, response );

        request =
            new RequestImpl(
                   requestId, RequestType.SORT, requestAttributes);

        System.out.println( "===============================================" );

        requestProcessor.processRequest( request, response );

        System.out.println( "===============================================" );
    }


    public static void main( String args[] ){

        ApplicationCommand application = new ApplicationCommand();
        application.runExample();
    }
}
